<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item :show-overflow-tooltip="true" label="单位" prop="organId">
        <el-input
          v-model="queryParams.organId"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经手人" prop="handsPersonId">
        <el-input
          v-model="queryParams.handsPersonId"
          placeholder="请输入经手人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="变动金额" prop="changeAmount">
        <el-input
          v-model="queryParams.changeAmount"
          placeholder="优惠/收款/付款/实付"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠金额" prop="discountMoney">
        <el-input
          v-model="queryParams.discountMoney"
          placeholder="请输入优惠金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合计金额" prop="totalPrice">
        <el-input
          v-model="queryParams.totalPrice"
          placeholder="请输入合计金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="账户" prop="accountId">
        <el-select
          placeholder="请选择账户"
          clearable
          @keyup.enter.native="handleQuery"
          v-model="queryParams.accountId"
        >
          <el-option
            v-for="(item, index) in accountList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="单据编号" prop="billNo">
        <el-input
          v-model="queryParams.billNo"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="单据日期" prop="billTime">
        <el-date-picker
          clearable
          v-model="queryParams.billTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择单据日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="单据来源" prop="source">
        <el-select
          placeholder="请选择单据来源"
          clearable
          @keyup.enter.native="handleQuery"
          v-model="queryParams.source"
        >
          <el-option label="PC" value="0" />
          <el-option label="手机" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="source">
        <el-select
          placeholder="请选择状态"
          clearable
          @keyup.enter.native="handleQuery"
          v-model="queryParams.status"
        >
          <el-option label="未审核" value="0" />
          <el-option label="已审核" value="1" />
          <el-option label="审核中" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          placeholder="请选择类型"
          clearable
          @keyup.enter.native="handleQuery"
          v-model="queryParams.type"
        >
          <el-option label="支出" value="1" />
          <el-option label="收入" value="2" />
          <el-option label="收款" value="3" />
          <el-option label="付款" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['account:accountHead:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['account:accountHead:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['account:accountHead:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['account:accountHead:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="accountHeadList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->

      <el-table-column
        label="单位"
        :show-overflow-tooltip="true"
        align="center"
        prop="organ"
      />
      <el-table-column
        label="经手人"
        :show-overflow-tooltip="true"
        align="center"
        prop="handsPerson"
      />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 1">支出</span>
          <span v-if="scope.row.type == 2">收入</span>
          <span v-if="scope.row.type == 3">收款</span>
          <span v-if="scope.row.type == 4">付款</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="变动金额"
        :show-overflow-tooltip="true"
        align="center"
        prop="changeAmount"
      /> -->
      <!-- <el-table-column
        label="优惠金额"
        :show-overflow-tooltip="true"
        align="center"
        prop="discountMoney"
      /> -->
      <el-table-column
        label="合计金额"
        :show-overflow-tooltip="true"
        align="center"
        prop="discountMoney"
      />
      <el-table-column
        label="账户"
        :show-overflow-tooltip="true"
        align="center"
        prop="account.name"
      />
      <el-table-column
        label="单据编号"
        :show-overflow-tooltip="true"
        align="center"
        prop="billNo"
      />
      <el-table-column label="单据日期" align="center" prop="billTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.billTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="附件名称" align="center" prop="fileName" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">未审核</span>
          <span v-if="scope.row.status == 1">已审核</span>
          <span v-if="scope.row.status == 2">审核中</span>
        </template>
      </el-table-column>
      <el-table-column label="单据来源" align="center" prop="source">
        <template slot-scope="scope">
          <span v-if="scope.row.source == 0">PC</span>
          <span v-if="scope.row.source == 1">手机</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['account:accountHead:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['account:accountHead:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改财务主对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单位" prop="organ">
          <el-input v-model="form.organ" placeholder="收款/付款单位" />
        </el-form-item>
        <el-form-item label="经手人" prop="handsPerson">
          <el-input v-model="form.handsPerson" placeholder="请输入经手人" />
        </el-form-item>
        <!-- <el-form-item label="变动金额" prop="changeAmount">
          <el-input v-model="form.changeAmount" placeholder="优惠/收款/付款/实付" />
        </el-form-item>
        <el-form-item label="优惠金额" prop="discountMoney">
          <el-input v-model="form.discountMoney" placeholder="请输入优惠金额" />
        </el-form-item> -->
        <el-form-item label="合计金额" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入合计金额" />
        </el-form-item>
        <el-form-item label="账户" prop="accountId">
          <el-select placeholder="请选择账户" v-model="form.accountId">
            <el-option
              v-for="(item, index) in accountList"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select placeholder="请选择类型" clearable v-model="form.status">
            <el-option label="未审核" value="0" />
            <el-option label="已审核" value="1" />
            <el-option label="审核中" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select placeholder="请选择类型" clearable v-model="form.type">
            <el-option label="支出" :value="1" />
            <el-option label="收入" :value="2" />
            <el-option label="收款" :value="3" />
            <el-option label="付款" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="单据编号" prop="billNo">
          <el-input v-model="form.billNo" placeholder="请输入单据编号" />
        </el-form-item>
        <el-form-item label="单据日期" prop="billTime">
          <el-date-picker
            clearable
            v-model="form.billTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择单据日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="附件名称" prop="fileName">
          <el-input v-model="form.fileName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="单据来源" prop="source">
          <el-select placeholder="请选择单据来源" v-model="form.source">
            <el-option label="PC" value="0" />
            <el-option label="手机" value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAccountHead,
  getAccountHead,
  delAccountHead,
  addAccountHead,
  updateAccountHead,
} from "@/api/account/accountHead";
import {
  listAccount,
  getAccount,
  delAccount,
  addAccount,
  updateAccount,
} from "@/api/account/account";

export default {
  name: "AccountHead",
  data() {
    return {
      accountList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 财务主表格数据
      accountHeadList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        organ: null,
        handsPerson: null,
        changeAmount: null,
        discountMoney: null,
        totalPrice: null,
        accountId: null,
        billNo: null,
        billTime: null,
        fileName: null,
        status: null,
        source: null,
      },
      // 表单参数
      form: {
        account: {
          name: null,
        },
      },
      // 表单校验
      rules: {},
      sourceName: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询财务主列表 */
    getList() {
      this.loading = true;
      listAccountHead(this.queryParams).then((response) => {
        this.accountHeadList = response.rows;
        this.total = response.total;
        listAccount().then((res) => {
          this.accountList = res.rows;
        });
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        organ: null,
        handsPerson: null,
        creator: null,
        changeAmount: null,
        discountMoney: null,
        totalPrice: null,
        accountId: null,
        billNo: null,
        billTime: null,
        remark: null,
        fileName: null,
        status: null,
        source: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.status = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加财务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAccountHead(id).then((response) => {
        this.form = response.data;
        console.log(this.form);
        this.open = true;
        this.title = "修改财务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAccountHead(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAccountHead(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除财务主编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAccountHead(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "account/accountHead/export",
        {
          ...this.queryParams,
        },
        `accountHead_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
